#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <filesystem>
#include <iostream>
#include <string>

using namespace std;
int main(int argc, char *argv[])
{
    // 程序停止后time秒后重启
    int time = 3;
    if (argc < 3)
    {
        std::cout << "using:./watchdog 3 ./register_server 20018" << std::endl;
        return 0;
    }
    // 启动子进程
    int pid = fork();
    if(pid<0){// 创建子进程失败
        return 0;
    }else if(pid==0){
        setsid();
        umask(0);
        time = atoi(argv[1]);
        string cmd = "";
        for(int i=2;i<argc;i++){
            cmd += " ";
            cmd += argv[i];
        }
        cout<<cmd<<endl;
        for(;;){
            int ret=system(cmd.c_str());
            sleep(time);
        }
    }else {// 父进程
        return 0;
    }
}